package com.aioz.security.controller;


import com.aioz.security.aop.annotation.LogInfoAnnotation;
import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.Company;
import com.aioz.security.entity.LoginUser;
import com.aioz.security.mapper.UserMapper;
import com.aioz.security.service.impl.CompanyInfoServiceImpl;
import com.aioz.security.service.impl.CompanyServiceImpl;
import com.aioz.security.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Aioz
 * @since 2022-09-27
 */
@RestController
@RequestMapping("/company")
@Slf4j
public class CompanyController {

    @Resource
    private CompanyServiceImpl companyService;

    @Resource
    private CompanyInfoServiceImpl companyInfoService;

    @Resource
    private UserMapper userMapper;

    //查询全部企业
    @PostMapping("/get/all/{currentPage}/{size}")
    public ResponseResult getAllCompanies(@PathVariable Integer currentPage,
                                          @PathVariable Integer size){
       return companyService.getAllCompanies(currentPage,size);
    }

    //查看高热度企业
    @PostMapping("/get/hot/{currentPage}")
    public ResponseResult getHotCompanies(@PathVariable Integer currentPage){
        return companyService.getHotCompanies(currentPage);
    }

    // 查看企业详细信息
    @PostMapping("/get/info/{id}")
    public ResponseResult getCompanyInfoById(@PathVariable Integer id){
        return companyInfoService.getInfo(id);
    }

    //根据用户Id查询企业信息
    @GetMapping("/get/by/userId")
    @LogInfoAnnotation
    public ResponseResult getInfoByUserId(){
        //获取登录用户Id
        Long userId = getCurrentUserId();
        Integer companyId = userMapper.getCompanyIdByUserId(userId);
        log.info("ID为 {} 的用户查看ID为 {} 的企业",userId,companyId);
        return companyInfoService.getInfo(companyId);
    }

    //更新企业信息
    @PostMapping("/update/info/{location}")
    @LogInfoAnnotation
    public ResponseResult updateCompanyInfo(@PathVariable(name = "location") String location,
                                            @RequestBody Company company){
        log.info("ID为 {} 的企业被更新信息",company.getId());
        return companyService.updateCompanyInfo(location,company);
    }

    //根据企业名称查询企业详细信息
    @PostMapping("/get/info/by/{name}")
    public ResponseResult getCompanyInfoByName(@PathVariable String name){
        return companyInfoService.getCompanyInfoByName(name);
    }

    //根据企业名称查询企业基本信息
    @PostMapping("/get/basic/info/{name}")
    public ResponseResult getBasicInfoByName(@PathVariable String name){
        return companyService.getBasicInfoByName(name);
    }

    public Long getCurrentUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getId();
    }

}
